import { message } from 'antd';
import update from 'immutability-helper';
import {
  queryProgramDet,
  queryMatList,
  auditProgramMat,
  auditProgram,
  delProgramMat,
  addProgMatSns,
} from '../services/api';

export default {
  namespace: 'list',

  state: {
    programInfo: {},
    matList: [],
  },

  effects: {
    *fetchProgramDet({ payload }, { call, put }) {
      const response = yield call(queryProgramDet, payload);
      yield put({
        type: 'queryList',
        payload: response.programDet,
      });
    },
    *fetchMatList({ payload }, { call, put }) {
      const response = yield call(queryMatList, payload);
      yield put({
        type: 'appendList',
        payload: response.programMats,
      });
    },
    *auditProgramMat({ payload }, { call, put }) {
      const { progId, matId, pmAuditStatus, matSn } = payload;
      const response = yield call(auditProgramMat, { progId, matId, pmAuditStatus, matSn });
      if (response?.result?.code === 2001) {
        message.error('提交出错，请检查');
      } else {
        yield put({
          type: 'updateList',
          payload,
        });
      }
    },
    *auditProgram({ payload, callback }, { call, put }) {
      const response = yield call(auditProgram, payload);
      if (response?.result?.code === 2001) {
        message.error('提交出错，请检查');
      } else if (callback) callback();
    },
    *delProgramMat({ payload, callback }, { call, put }) {
      // const { matId } = payload;
      const response = yield call(delProgramMat, payload);
      if (response?.result?.code === 2001) {
        message.error('删除失败，请检查');
      } else {
        yield put({
          type: 'saveDelProgramMat',
          payload,
        });
        if (callback) callback();
      }
    },
    *resort({ payload }, { call, put }) {
      yield put({
        type: 'resortList',
        payload,
      });
    },
    *addProgMatSns({ payload, callback }, { call }) {
      const response = yield call(addProgMatSns, payload);
      if (response?.result?.code === 2001) {
        message.error('提交审核失败，请检查');
      } else {
        if (callback) callback();
      }
    },
    *changeMatSn({ payload }, { put }) {
      yield put({
        type: 'changeMatSnOrder',
        payload,
      });
    },
  },

  reducers: {
    queryList(state, action) {
      return {
        ...state,
        programInfo: action.payload,
      };
    },
    appendList(state, action) {
      return {
        ...state,
        matList: action.payload,
      };
    },
    updateList(state, action) {
      //console.log('before old list', state.matList);
      // const newList = state.matList.slice(); // 深拷贝
      const { pmAuditStatus, matId, matSn } = action.payload;
      const matindex = state.matList.findIndex(item => item.matId === matId);
      //console.log('index & pm', matindex, pmAuditStatus);
      const newList = Object.assign([...state.matList]);
      if (matindex > -1) {
        newList[matindex].pmAuditStatus = pmAuditStatus;
        newList[matindex].matSn = matSn;
      }
      //console.log('after,list&newlist', state.matList, newList);
      return {
        ...state,
        matList: matindex > -1 ? newList : state.matList,
      };
    },
    saveDelProgramMat(state, action) {
      const newlist = state.matList.filter(item => action.payload.matId != item.matId);
      return {
        ...state,
        matList: newlist,
      };
    },
    changeMatSnOrder(state, action) {
      const newindex = state.matList.findIndex(item => item.matId === action.payload.row.matId);
      //console.log('newindex', newindex);
      const newState = update(state, {
        matList: {
          [newindex]: { matSn: { $set: action.payload.value } },
        },
      });
      //console.log('newstate', newState);
      return newState;
    },

    resortList(state, action) {
      const { dragIndex, hoverIndex } = action.payload;
      const dragRow = state.matList[dragIndex];
      const newState = update(state, {
        matList: {
          $splice: [
            [dragIndex, 1],
            [hoverIndex, 0, dragRow],
          ],
        },
      });
      return newState;
    },
  },
};
